/**
 * @description:
 * @author: 梅雄
 * @createDate: 2022/1/19
 * @version: 1.0
 * @status: done
 */
package com.xhwl.centiotdata.suppliersystem.eesd.hwweather;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.xhwl.centiotdata.pojo.dto.eesd.EesdConfigDTO;
import com.xhwl.centiotdata.pojo.enums.eesd.EesdMethodEnum;
import com.xhwl.centiotdata.service.cent.IBiEnvCentService;
import com.xhwl.centiotdata.service.integrateconfig.EESDIntegrationConfig;
import com.xhwl.centiotdata.utils.RedisUtils;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.bi.DwdFactEnvironmentMetrics;
import com.xhwl.common.utils.ChkUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Copyright: Copyright (c) 2021  ALL RIGHTS RESERVED.
 * @Company: 深圳兴海物联科技有限公司
 * @Author: 梅雄
 * @CreateDate: 2022/1/19 9:27
 * @UpdateDate: 2022/1/19 9:27
 * @UpdateRemark: init
 * @Version: 1.0
 * @menu
 */
@RestController
@Slf4j
public class HwWeatherHandler {

    @Autowired
    private EESDIntegrationConfig eesdIntegrationConfig;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    IBiEnvCentService iBiEnvCentService;

    /**
     * 天气缓存的KEY
     */
    private final static String REDIS_ENV_KEY = "HUAWEI:WEATHER";
    /**
     * 缓存10分钟
     */
    private final static int REDIS_ENV_TIME = 10;

    /**
     * 查询实时天气情况
     */
    @GetMapping(value = "/weather/get")
    public ResultJson weatherGet() {
        List<EesdConfigDTO> integrationConfig = eesdIntegrationConfig.getIntegrationConfig(EesdMethodEnum.WEATHER.getId());
        if (ChkUtil.isNull(integrationConfig)) {
            throw new RuntimeException("配置无法获取");
        }
        Map res = (Map) redisUtils.get(REDIS_ENV_KEY);
        if (ChkUtil.isNotNull(res)) {
            return ResultJson.success(res);
        }

        Map<String, Object> result = HwWeatherHandlerUtil.getRes(integrationConfig);
        if (ChkUtil.isNotNull(result.get("temp")) && ChkUtil.isNotNull(result.get("pm2p5"))) {
            redisUtils.setWithMinute(REDIS_ENV_KEY, result, REDIS_ENV_TIME);
        }
        return ResultJson.success(result);
    }

    /**
     * 定时任务一个小时更新一次数据
     */
    public List weatherTask(EesdConfigDTO eesdConfigDTO) {
        log.info("==================3333333333333333333333333333333333333333执行阿里天气统计正式开始.....");
        Map<String, Object> result = HwWeatherHandlerUtil.getRes(Collections.singletonList(eesdConfigDTO));
        Integer projectId = ChkUtil.getInteger(result.get("projectId"));
        String temp = ChkUtil.getString(result.get("temp"));
        String humidity = ChkUtil.getString(result.get("humidity"));
        String pm2p5 = ChkUtil.getString(result.get("pm2p5"));
        String windScale = ChkUtil.getString(result.get("windScale"));

        //处理 温/湿度 pm25
        List<DwdFactEnvironmentMetrics> lists = Lists.newArrayList();
        if (ChkUtil.isNotNull(result.get("temp"))) {
            DwdFactEnvironmentMetrics tempInfo = HwWeatherHandlerUtil.getInfo(projectId, "temp", Double.valueOf(temp));
            lists.add(tempInfo);
        }
        if (ChkUtil.isNotNull(result.get("humidity"))) {
            DwdFactEnvironmentMetrics humidityInfo = HwWeatherHandlerUtil.getInfo(projectId, "humidity", Double.valueOf(humidity));
            lists.add(humidityInfo);
        }
        if (ChkUtil.isNotNull(result.get("pm2p5"))) {
            DwdFactEnvironmentMetrics pm2p5Info = HwWeatherHandlerUtil.getInfo(projectId, "pm2p5", Double.valueOf(pm2p5));
            lists.add(pm2p5Info);
        }
        if (ChkUtil.isNotNull(result.get("category"))) {
            String[] split = ((String) result.get("category")).split("/");
            DwdFactEnvironmentMetrics categoryInfo = HwWeatherHandlerUtil.getInfo(projectId, "category", Double.valueOf(split[0]));
            lists.add(categoryInfo);
        }
        if (ChkUtil.isNotNull(result.get("windScale"))) {
            DwdFactEnvironmentMetrics windLevelInfo = HwWeatherHandlerUtil.getInfo(projectId, "windScale", Double.valueOf(windScale));
            lists.add(windLevelInfo);
        }
        if (ChkUtil.isNotNull(lists)) {
            iBiEnvCentService.asyncEnvInitData(JSON.toJSONString(lists));
        }
        return lists;
    }
}
